package com.gy.common.util;

import com.gy.common.market.SelectField;
import com.gy.common.model.vo.SysDictVo;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

public class GetDictField {
    public static <T> void getFieldChinese(List<T> list, List<SysDictVo> sysDicts) {
        for (T t : list) {
            Field[] fields = t.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                SelectField selectField = field.getAnnotation(SelectField.class);
                if (selectField != null && !StringUtils.isEmpty(selectField.select())) {
                    for (SysDictVo sysDictVo : sysDicts) {
                        try {
                            if (sysDictVo.getDictType().equals(selectField.select()) && Objects.equals(field.get(t), sysDictVo.getDictId())) {
                                field.set(t, sysDictVo.getDictName());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
